﻿using Face8280WebSocketServer.Command;
using DoNetDrive.Core.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoNetDrive.Core.Packet;
using Newtonsoft.Json.Serialization;

namespace Face8280WebSocketServer.Command.Person
{
    public class GetPerson : JsonCommand
    {
        public GetPerson(INCommandDetail cd, uint code) : base(cd, null)
        {
            mSendObject = new { cmd = Reply, code };
            //  mJsonResult = (s) => _Result = new GetPersonResult(s);
        }
        protected override void CommandNext(INPacket readPacket)
        {
            if (readPacket is JsonPacket)
            {
                var jPck = readPacket as JsonPacket;
                var sJson = jPck.GetJson();
                if (CheckJsonResult(sJson) == false)
                {
                    return;
                }
                var person = sJson.FromJson<GetPersonResponse>();
                var file = jPck.GetBinaryData();
                if (file != null)
                {
                    person.FeatureData.Sort((x, y) => x.Num.CompareTo(y.Num));
                    int len = 0;
                    foreach (var item in person.FeatureData)
                    {
                        item.BinaryData = new byte[item.Len];
                        Array.Copy(file, len, item.BinaryData, 0, item.Len);
                        len += item.Len;
                    }
                }
                _Result = new GetPersonResult(person);
                CommandCompleted();
            }
        }
    }
}
